package cn.yeyiu.netonlinevideo.parser.manga;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import cn.yeyiu.netonlinevideo.bean.SourceData;
import cn.yeyiu.netonlinevideo.bean.TypeData;
import cn.yeyiu.netonlinevideo.parser.Parser;
import cn.yeyiu.netonlinevideo.viewmodel.DataListViewModel;
import cn.yeyiu.util.NetUtil;
import cn.yeyiu.util.bean.MangaEP;
import cn.yeyiu.util.bean.MangaDetail;
import cn.yeyiu.util.bean.MediaData;

public class JComicParser extends Parser {
    public JComicParser(SourceData source) {
        super(source);
    }

    @Override
    public List<TypeData> getTypeList() {
        String html = NetUtil.getByUrl(source.url);
        Document document = Jsoup.parse(html);
        Elements elements = document.getElementsByClass("col-xs-4 col-md-2 portfolio-item");

        List<TypeData> dataList = new ArrayList<>();
        TypeData data;
        /*
        <div class="col-xs-4 col-md-2 portfolio-item">
                <a href="/cat/禁書目錄">
                    <img class="img-responsive" src="/cat/禁書目錄.jpg" alt="">
                </a>
                <h3>
                    <a href="/cat/禁書目錄">禁書目錄</a>
                </h3>

            </div>
         */
        for (Element element : elements) {
            data = new TypeData();
//            data.code = source.code;
//            data.type = source.type;

            String img = element.selectXpath("./div/a/img").first().attr("src");
            String title = element.selectXpath("./div/h3/a").first().text();

            data.name = title;
            data.logo = source.url + img;

            dataList.add(data);
        }
        return dataList;
    }

    @Override
    public Object getBaseData(String type, int pageIndex) {
        String url = source.url + "/cat/" + type + "/" + (pageIndex + 1);
        String html = NetUtil.getByUrl(url);
        return Jsoup.parse(html);
    }

    @Override
    public int getMaxCount(Object data) {
        Document document = (Document) data;
        String pageCount = document.selectXpath("//ul[@class='pagination']/li[last()-1]/a").first().text();
        return Integer.parseInt(pageCount);
    }

    @Override
    public List<MediaData> getDataList(Object data, String type) {
        Document document = (Document) data;
        Elements elements = document.getElementsByClass("row col-lg-4 col-md-6 col-xs-12");

        List<MediaData> dataList = new ArrayList<>();
        MediaData mediaData;
        for (Element element : elements) {
            mediaData = new MediaData();
            mediaData.code = source.code;
            mediaData.type = source.type;

            String img = element.selectXpath("./div/a/div/img").first().attr("src");
            String title = element.selectXpath("./div/div/a/p[1]").first().text();
            String name = element.selectXpath("./div/a").first().attr("href");
            int idx = name.indexOf("/", 4);
            name = name.substring(idx + 1);

            mediaData.title = title;
            mediaData.logo = source.url + img;
            mediaData.id = name;

            dataList.add(mediaData);
        }
        return dataList;
    }

    @Override
    public Object buildData(MediaData mediaData) {
        MangaDetail mangaDetail = new MangaDetail();
        mangaDetail.setTitle(mediaData.title);
        mangaDetail.setLogo(mediaData.logo);

        // get eps
        String html = NetUtil.getByUrl(source.url + "/eps/" + mediaData.id);
        Document document = Jsoup.parse(html);

        Elements div = document.getElementsByClass("row col-md-6 col-xs-12");
        Elements ul = div.get(1).getElementsByTag("a");

        List<MangaEP> mangaEPs = new ArrayList<>();
        MangaEP mangaEP;
        for (Element li : ul) {
            mangaEP = new MangaEP();

            // get eps
            String pageUrl = source.url + li.attr("href");
            html = NetUtil.getByUrl(pageUrl);
            document = Jsoup.parse(html);
            Elements imgs = document.getElementsByClass("lazy comic-view");

            // update
            mangaEP.setTitle(li.selectXpath("./a/button").first().text());
            // this site no logo for ep
            mangaEP.setLogo(mediaData.logo);
            mangaEP.picList = new ArrayList<>();
            for (Element img : imgs) {
                mangaEP.picList.add(source.url + img.attr("data-original"));
            }

            mangaEPs.add(mangaEP);
        }
        mangaDetail.setEps(mangaEPs);
        return mangaDetail;
    }
}
